<?php
defined('_JEXEC') or die();

class WrdControllerMservices extends JControllerLegacy 
{
        protected $option;
        protected $controller; 
		function __construct( $default = array()){
    		$this->option = 'com_wrd';
    		$this->controller = 'mservices';
			parent::__construct( $default );
    	}

      	function display()
 		{
			switch($this->getTask()){
				case 'add':{
						JRequest::setVar( 'hidemainmenu', 1 );
						JRequest::setVar( 'layout', 'form'  );
						JRequest::setVar( 'view'  , 'mservices');
					} 
					break;
	            default:
					JRequest::setVar( 'view'  , 'mservices');
	            	break;
			}
			parent::display();
        }
        
      //save profile info
    function save(){
       
	   $app = JFactory::getApplication() ;  
       $post=JRequest::get('post');
	  // echo "<pre>";print_r($post);die;
       $post['created_date'] = date("Y-m-d H:i:s");
       $params = JComponentHelper::getParams('com_wrd');
       $model = $this->getModel('mservices');
       
       /**** Check member type and date ****/
       $post['member_type'] = intval($post['topspenders']).'_'.intval($post['newjoinmember']).'_'.intval($post['inactivemembers']).'_'.intval($post['daily_top_spender']);
       if(intval($post['topspenders']) <= 0){
           $post['top_spender_date'] = '';
       } 
       if(intval($post['newjoinmember']) <= 0){
           $post['new_join_member_date'] = '';
       } 
       if(intval($post['inactivemembers']) <= 0){
           $post['inactive_member_date'] = '';
       }
       if(intval($post['daily_top_date']) <= 0){
           $post['daily_top_date'] = '';
       }
	   //get date current
		//echo '<pre>';print_r($post);die;
       $id_category = $model->store($post,'mservices');
       if($id_category!=0){
			/*
			if(!empty($post['complain_flag'])){
				//exit();
				if($post['complain_flag']){
					$this->setRedirect('index.php?option=com_wrd&controller=profile&id='.$post['complain_flag'].'&layout=form&tab=memberservices');
				}else{
					$this->setRedirect('index.php?option=com_wrd&controller=profile&id=31691&layout=form&tab=memberservices','Saved');
				}
				$app->setUserState('complain_flag', '') ;
				$app->setUserState('user_id', '') ;
			}else{
       			$this->setRedirect('index.php?option=com_wrd&controller=profile&id=31691&layout=form&tab=memberservices','Saved');
			}
			*/
			$this->setRedirect('index.php?option=com_wrd&controller=profile&id='.$post['complain_flag'].'&layout=form&tab=memberservices','Saved');
			return true;                
       }
       else{
            $this->setRedirect('index.php?option=com_wrd&controller=profile&id=31691&layout=form&tab=memberservices','Error Saved');
            return false;
       }
    }    
    function cancel(){
		$post=JRequest::get('post');
		//echo '<pre>';print_r($post);die;
		$this->setRedirect('index.php?option=com_wrd&controller=profile&id='.$post['complain_flag'].'&layout=form&tab=memberservices');
	}
    function remove()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to delete' ) );
		}

		$model = $this->getModel('mservices');
		if(!$model->delete($cid)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_wrd&controller=mservices' );
	}
   
    function publish()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( '<p style="color:red">Please select an item to publish</p>' ) );
		}

		$model = $this->getModel('mservices');
		if(!$model->publish($cid, 1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_wrd&controller=mservices' );
	}

	function unpublish(){
		
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( '<p style="color:red">Please select an item to publish</p>'  ) );
		}

		$model = $this->getModel('mservices');
		
		if(!$model->publish($cid, 0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		
		$this->setRedirect( 'index.php?option=com_wrd&controller=mservices' );
	}
}